#include <cstdlib>
#include <cstdio>
#include "Parser.h"
#include "Scanner.h"

/* INCLUDES SOURCE FILE - INCLUDE ONLY HERE */
#include "MosParser.inc"

using namespace moses;

std::string oldp;
std::string stringBuffer;

Parser::Parser(Scanner &scanner)
: m_scanner(scanner)
{
	m_pParser = ::ParseAlloc(malloc);
}

Parser::~Parser()
{
	::ParseFree(m_pParser, free);
}

Item* Parser::BuildTree()
{
	int token;

	ParseState state;

	while ((token = m_scanner.GetNextToken()) != 0)
	{
		::Parse(m_pParser, token, 0, &state);
		oldp = yytext;	// TODO: might slow things down significantly. Problem
						// is that using yytext directly in moses.y will get the
						// wrong token (the token after the one wanted)
	}

	::Parse(m_pParser, 0, 0, (ParseState*)&state);

	return state.pTree;
}
